package com.example.administrator.asynchronoustool;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.asynchronoustool.AsynchronousTools;
import com.example.administrator.asynchronoustool.R;

import rx.Observable;

public class MainActivity extends AppCompatActivity implements AsynchronousTools.ToDoTimeConsumingListener, AsynchronousTools.MainThreadOperationListener, View.OnClickListener {
    private AsynchronousTools as = new AsynchronousTools();
    private Button bu1;
    private Button bu2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bu1 = (Button) findViewById(R.id.demo1);
        bu2 = (Button) findViewById(R.id.demo2);
        bu1.setOnClickListener(this);
        bu2.setOnClickListener(this);
        as.setToDoTimeConsumingListener(this);
        as.setMainListener(this);

    }

    @Override
    public void toDoTimeConsuming(String state) {
        if (state.equals("1")) {
            Log.e("pppppppppppppppppp", "按钮1支线程启动!");
        }
        if (state.equals("2")) {
            Log.e("oooooooooooooooo", "按钮2支线程启动!");
        }
    }

    @Override
    public void mainThreadOperation(String state) {
        Toast.makeText(this, state, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.demo1:
                as.toDoTimeConsuming("1");
                as.mainThreadOperation();
                break;
            case R.id.demo2:
                as.toDoTimeConsuming("2");
                as.mainThreadOperation();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //  防止内存泄漏
        as.release();
    }
}
